# from selenium import webdriver
# from selenium.webdriver.common.action_chains import ActionChains
# class MyClass(object):
#     def __init__(self):
#         self.web = webdriver.Chrome()
#         self.web.implicitly_wait(10)
#         url = 'https://webvpn.nankai.edu.cn/http/77726476706e69737468656265737421f5f64c95347e6651700388a5d6502720dc08a5/eams/myPlan!programDoc.action'
#         self.web.get(url)
#         self.is_continue=True
#     def log_in(self):
#         username=input('请输入学号：')
#         password=input('请输入密码：')
#         self.web.find_element_by_xpath('/html/body/div/div[2]/div[1]/div[2]/ul/li[2]/input').send_keys(username)
#         self.web.find_element_by_xpath('/html/body/div/div[2]/div[1]/div[2]/ul/li[3]/input').send_keys(password)
#
#         ac = ActionChains(self.web)
#         ac.drag_and_drop_by_offset(self.web.find_element_by_xpath('//*[@id="btn"]'), 250, 0).perform()
#     def to_page(self):
#         self.web.find_element_by_xpath('//*[@id="submitRole"]').click()
#         self.web.find_element_by_xpath('//*[@id="menu_panel"]/ul/li[1]/ul/div/li[3]/a').click()
#         self.web.find_element_by_xpath('//*[@id="ui-id-2"]').click()
#         self.mainWindow = self.web.current_window_handle
#         print('输入"over"结束程序！')
#     def to_new(self):
#         self.mainWindow = self.web.current_window_handle
#         self.name = input('输入课程名称：')
#         if self.name=='over':
#             self.web.quit()
#             print('完成查找！开学快乐！')
#             self.is_continue=False
#             return
#         try:
#             self.web.find_element_by_partial_link_text(self.name).click()
#             self.new_window = self.web.window_handles[-1]
#             self.is_pass = True
#         except:
#             self.is_pass = False
#             print('ERROR！未查找到相关课程！请重新输入！')
#     def get_info(self):
#         self.to_new()
#         while (self.is_continue):
#             if self.is_pass:
#                 self.web.switch_to.window(self.new_window)
#                 with open('教材名查找.txt', 'a')as file:
#                     # print(web.page_source)
#                     try:
#                         book_name=self.web.find_element_by_xpath('/html/body/table/tbody/tr[1]/td[2]').text
#                         info = self.web.find_element_by_xpath('/html/body/table/tbody/tr[9]/td[2]').text
#                     except:
#                         #self.web.find_element_by_xpath('/html/body/table/tbody/tr[5]/td[2]/a').click()
#                         info = '该课程未显示所用教材'
#                     file.write(book_name + '--------所用教材：' + info + '\n')
#                     file.write(
#                         '.............................................................................................\n')
#                 self.web.close()
#                 self.web.switch_to.window(self.mainWindow)
#             else:
#                 pass
#             self.to_new()
#     def run(self):
#         self.log_in()
#         self.to_page()
#         self.get_info()
# class_book = MyClass()
# class_book.run()
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import selenium.webdriver.support.expected_conditions as EC
from PIL import Image
from PIL import ImageOps
from PIL import ImageFilter
from io import BytesIO
from time import sleep
import matplotlib.pyplot as plt
import cv2 as cv
from selenium.webdriver.support.ui import WebDriverWait
class MyClass(object):
    def __init__(self):
        self.web = webdriver.Chrome()
        self.web.implicitly_wait(30)
        self.web.maximize_window()
        self.url='http://mifan.61.com/'
        self.web.get(self.url)
    def login(self):
        iframe = self.web.find_element_by_xpath('/html/body/div/iframe')
        self.web.switch_to.frame(iframe)#切换到登录的frame
        username=input('请输入米米号：')
        password=input('请输入密码：')

        input_ = self.web.find_element_by_xpath('//*[@id="app"]/div[1]/div[2]/form/div[1]/input[1]')
        input_.send_keys(username)
        input_ = self.web.find_element_by_xpath('//*[@id="app"]/div[1]/div[2]/form/div[1]/input[2]')
        input_.send_keys(password)
        btn = self.web.find_element_by_xpath('//*[@id="app"]/div[1]/div[2]/form/div[1]/button')
        btn.click()  # 登录
    def identify(self):
        iframe = self.web.find_element_by_xpath('//*[@id="tcaptcha_iframe"]')
        self.web.switch_to.frame(iframe)  # 切换到验证码
        self.slide = self.web.find_element_by_xpath('//*[@id="tcaptcha_drag_thumb"]')
        #sleep(5)
        self.is_continue = True
        while (self.is_continue):
            # try:
            img1 = self.web.find_element_by_xpath('//*[@id="tcOperation"]/div[1]').screenshot_as_png
            # except:
            #     break
            cut_pic = Image.open(BytesIO(img1))
            out_im = cut_pic.filter(ImageFilter.SHARPEN)#锐化

            out_im.save('over.png')
            image = cv.imread('over.png')
            canny = cv.Canny(image, 200, 400)
            contours, hierarchy = cv.findContours(canny, cv.RETR_CCOMP, cv.CHAIN_APPROX_SIMPLE)
            for i, contour in enumerate(contours):
                x, y, w, h = cv.boundingRect(contour)
                cv.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
            #plt.imshow(image)
            #plt.show()
            #self.target=0
            for i, contour in enumerate(contours):
                if cv.contourArea(contour) > 1600 and cv.contourArea(contour) <= 3500 and cv.arcLength(contour, True) > 160 and cv.arcLength(contour, True) < 500:
                    x, y, w, h = cv.boundingRect(contour)
                    if x <= 200:
                        continue
                    else:
                        # self.target=x
                        self.dragslide(x)
                        break
            try:
                EC.element_to_be_clickable(self.slide)#没过
                self.web.find_element_by_xpath('//*[@id="reload"]/div').click()  # 刷新验证码图片
                sleep(5)
                continue
            except:
                self.sign_in()
                break
    def dragslide(self,x):
        self.ac = ActionChains(self.web)
        self.ac.click_and_hold(self.slide).perform()
        self.ac.move_to_element_with_offset(self.slide, x - 3, 0).perform()
        self.slide.click()
        sleep(3)
    def sign_in(self):
        self.web.switch_to.frame(self.web.find_element_by_xpath('/html/body/div/iframe'))
        #self.ac = ActionChains(self.web)
        self.web.find_element_by_xpath('//*[@id="app"]/div[1]/div[2]/ul/li[5]').click()
        self.web.find_element_by_xpath('//*[@id="home-infos"]/div[2]/p[2]/span[3]').click()
    def run(self):
        self.login()
        self.identify()
        sleep(5)
        self.web.quit()
showtime = MyClass()
showtime.run()
